﻿using UnityEngine;
using System.Collections;
using System.Collections;

namespace ChuMeng
{
	public enum AIStateEnum
	{
		INVALID = -1,
		IDLE,
		COMBAT,
		DEAD,
		MOVE,

		FLEE,
		KNOCK_BACK,

		CastSkill,
	}

	public class AIState
	{
		protected AICharacter aiCharacter;
		protected bool quit = false;

		public void SetChar (AICharacter a)
		{
			aiCharacter = a;
		}

		public AIStateEnum type = AIStateEnum.INVALID;

		public AIState ()
		{
		
		}

		/// <summary>
		///重载的时候 子类需要在while中判定是否quit状态
		/// </summary>
		public virtual IEnumerator RunLogic ()
		{
			while (!quit) {
				yield return null;
			}
		}

		public static AIState CreateState (AIStateEnum s)
		{
			switch (s) {
			case AIStateEnum.IDLE:
				return new IdleState ();
			case AIStateEnum.COMBAT:
				return new CombatState ();
			case AIStateEnum.DEAD:
				return new DeadState ();
			case AIStateEnum.MOVE:
				return new MoveState ();
			default:
				return null;
			}
		}

		/// <summary>
		/// 重载的时候 子类 需要 执行父类的enterState exitState 函数
		/// </summary>
		public virtual void EnterState ()
		{
			quit = false;
		}
		/// <summary>
		/// 重载的时候 子类 需要 执行父类的enterState exitState 函数
		/// </summary>
		public virtual void ExitState ()
		{
			quit = true;
		}

		public virtual bool CheckNextState (AIStateEnum next)
		{
			return true;
		}

		protected void PlayAni (string name, float speed, WrapMode wm)
		{
			aiCharacter.PlayAni (name, speed, wm);
		}

		protected void SetAni (string name, float speed, WrapMode wm)
		{
			aiCharacter.SetAni (name, speed, wm);
		}

		protected bool CheckAni (string name)
		{
			return aiCharacter.GetAttr ().animation.GetClip (name) != null;
		}

		
		protected NpcAttribute GetAttr ()
		{
			return aiCharacter.GetAttr ();
		}

		protected MyAnimationEvent GetEvent ()
		{
			return GetAttr ().GetComponent<MyAnimationEvent> ();
		}

		

		protected bool CheckFlee ()
		{
			if (GetEvent ().fleeEvent && (Time.time - GetEvent ().fleeTime) < 0.1f) {
				GetEvent ().fleeEvent = false;
				return true;
			} else {
				GetEvent ().fleeEvent = false;
			}
			return false;
		}


		
		protected bool CheckBaseEvent ()
		{
			
			return false;
		}

		protected bool CheckAttackEvent() {
			
			return false;
		}

		//TODO:检测一些事件 然后进行状态切换
		//获得对应注册哪些事件， 就检测哪些事件？
		//只有状态切换成功才回 CheckEvent 返回true
		protected bool CheckEvent ()
		{
			if (CheckBaseEvent ()) {
				return true;
			}
			return CheckAttackEvent ();
		}

	}

	public class IdleState  : AIState
	{
		public IdleState ()
		{
			type = AIStateEnum.IDLE;
		}

	}

	public class CombatState : AIState
	{
		public CombatState ()
		{
			type = AIStateEnum.COMBAT;
		}
	}

	public class MoveState : AIState
	{
		public MoveState ()
		{
			type = AIStateEnum.MOVE;
		}

	}

	public class DeadState : AIState
	{
		public DeadState ()
		{
			type = AIStateEnum.DEAD;
		}

		public override bool CheckNextState (AIStateEnum next)
		{
			return false;
		}
	}

	public class FleeState : AIState
	{
		public FleeState ()
		{
			type = AIStateEnum.FLEE;
		}
	}

	public class KnockBackState : AIState
	{
		public KnockBackState ()
		{
			type = AIStateEnum.KNOCK_BACK;
		}
	}

	public class SkillState : AIState
	{
		public SkillState ()
		{
			type = AIStateEnum.CastSkill;
		}
		public override bool CheckNextState (AIStateEnum next)
		{
			if (next == AIStateEnum.CastSkill) {
				return false;
			}
			if (next == AIStateEnum.COMBAT) {
				return false;
			}
			return base.CheckNextState (next);
		}
	}
}